package com.macro.mall.controller;

import com.macro.mall.common.annotation.AccessLimit;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.SmsDictionary;
import com.macro.mall.service.SmsDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典管理Controller
 *
 * @author 郎
 */
@Controller
@Api(tags = "SmsDictionaryController")
@Tag(name = "SmsDictionaryController", description = "字典管理")
@RequestMapping("/dict")
@AllArgsConstructor
public class SmsDictionaryController {

    private final SmsDictionaryService smsDictionaryService;

    @AccessLimit
    @ApiOperation("查询所有字典数据")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Object> listAll() {
        return CommonResult.success(smsDictionaryService.listAll());
    }

    @AccessLimit
    @ApiOperation("根据字典类型加载字典项")
    @RequestMapping(value = "/getListByType", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Object> getListByType(String codeType) {
        return CommonResult.success(smsDictionaryService.getListByType(codeType));
    }

    @AccessLimit
    @ApiOperation("分页查询所有字典")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SmsDictionary>> list(String code, String name, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<SmsDictionary> list = smsDictionaryService.list(code, name, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @AccessLimit
    @ApiOperation("根据pid分页查询子项字典")
    @RequestMapping(value = "/listChildren", method = RequestMethod.GET)
    @ResponseBody
    CommonResult<CommonPage<SmsDictionary>> listChildren(Long pid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<SmsDictionary> list = smsDictionaryService.listChildren(pid, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @AccessLimit
    @ApiOperation("删除当前字典(通常用于删除字典的子项)")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<Object> delete(@PathVariable("id") Long id) {
        int count = smsDictionaryService.delete(id);
        return count > 0 ? CommonResult.success(count) : CommonResult.failed();
    }

    @AccessLimit
    @ApiOperation("删除当前字典及其子项(通常用于删除字典项)")
    @RequestMapping(value = "/deleteCurrentAndChildren/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<Object> deleteCurrentAndChildren(@PathVariable("id") Long id) {
        int count = smsDictionaryService.deleteCurrentAndChildren(id);
        return count > 0 ? CommonResult.success(count) : CommonResult.failed();
    }

    @AccessLimit
    @ApiOperation("新增字典")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> create(@RequestBody SmsDictionary smsDictionary) {
        int count = smsDictionaryService.create(smsDictionary);
        return count > 0 ? CommonResult.success(count) : CommonResult.failed();
    }

    @AccessLimit
    @ApiOperation("修改字典")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult<Object> update(@RequestBody SmsDictionary smsDictionary) {
        int count = smsDictionaryService.update(smsDictionary);
        return count > 0 ? CommonResult.success(count) : CommonResult.failed();
    }

    @AccessLimit
    @ApiOperation("根据ID获取字典详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Object> detail(@PathVariable("id") Long id) {
        return CommonResult.success(smsDictionaryService.detail(id));
    }
}
